package com.fhc;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;

import com.vladimirjosu.util.Util;

public class Pixels implements Runnable {
	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer st;
	static Random rnd;

	void solve() throws IOException {
		int tests = nextInt();

		for (int test = 1; test <= tests; test++) {
			solveOne(test);
		}
	}

	@SuppressWarnings("unchecked")
	private void solveOne(int test) throws IOException {
		final int w = nextInt(), h = nextInt();
		final int p = nextInt(), q = nextInt();
		final int n = nextInt(), x = nextInt(), y = nextInt();
		final int a = nextInt(), b = nextInt(), c = nextInt(), d = nextInt();
		int[] xs = new int[n], ys = new int[n];
		xs[0] = x;
		ys[0] = y;
		for (int i = 1; i < n; i++) {
			xs[i] = (xs[i - 1] * a + ys[i - 1] * b + 1) % w;
			ys[i] = (xs[i - 1] * c + ys[i - 1] * d + 1) % h;
		}
		ArrayList<Integer>[] ysByX = new ArrayList[w];
		for (int i = 0; i < w; i++) {
			ysByX[i] = new ArrayList<Integer>();
		}
		for (int i = 0; i < n; i++) {
			int xc = xs[i], yc = ys[i];
			ysByX[xc].add(yc);
		}

		int[] sumsByY = new int[h], countByY = new int[h];

		// prepare first
		for (int i = 0; i < p; i++) {
			for (int yc : ysByX[i]) {
				countByY[yc]++;
			}
		}
		updateSums(h, countByY, sumsByY);

		// iterate
		int res = 0;
		for (int i = 0; i <= w - p; i++) {
			// update ans
			for (int j = 0; j <= h - q; j++) {
				int curSum = getSum(sumsByY, j, j + q - 1);

				if (curSum == 0) {
					++res;
				}
			}

			// need to update
			if (i != w - p) {
				for (int yc : ysByX[i]) {
					countByY[yc]--;
				}
				for (int yc : ysByX[i + p]) {
					countByY[yc]++;
				}

				updateSums(h, countByY, sumsByY);
			}
		}

		out.println("Case #" + test + ": " + res);
	}

	private int getSum(int[] sums, int l, int r) {
		int res = sums[r];

		if (l > 0) {
			res -= sums[l - 1];
		}

		return res;
	}

	private void updateSums(int n, int[] count, int[] sums) {
		sums[0] = count[0];

		for (int i = 1; i < n; i++) {
			sums[i] = sums[i - 1] + count[i];
		}
	}

	public static void main(String[] args) {
		new Pixels().run();
	}

	public void run() {
		try {
			final String className = this.getClass().getName().toLowerCase();

			try {
				// in = new BufferedReader(new FileReader(className + ".in"));
				in = new BufferedReader(new InputStreamReader(
						Util.getInputStream("input.txt")));

				out = new PrintWriter(new FileWriter("output/" + className
						+ ".out"));
				// in = new BufferedReader(new FileReader("input.txt"));
				// out = new PrintWriter(new FileWriter("output.txt"));
			} catch (FileNotFoundException e) {
				in = new BufferedReader(new InputStreamReader(System.in));
				out = new PrintWriter(System.out);
			}

			rnd = new Random();

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(42);
		}
	}

	String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String line = in.readLine();

			if (line == null)
				return null;

			st = new StringTokenizer(line);
		}

		return st.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}